<template>
  <div class="login-container">
    <van-nav-bar title="登录" class="page-nav-bar">
      <van-icon slot="left" name="cross" @click="$router.back()"></van-icon>
    </van-nav-bar>
    <!-- 表单 -->
    <van-form @submit="onSubmit" ref="loginFormRef">
      <van-field
        v-model="user.mobile"
        name="mobile"
        placeholder="请输入手机号"
        :rules="userFormRules.mobile"
        type="number"
        maxlength="11"
      >
        <!-- 手机图标插槽 -->
        <i slot="left-icon" class="toutiao toutiao-shouji"></i>
      </van-field>
      <van-field
        v-model="user.code"
        name="code"
        placeholder="请输入验证码"
        :rules="userFormRules.code"
        type="number"
        maxlength="6"
      >
        <!-- 验证码图标插槽 -->
        <i slot="left-icon" class="toutiao toutiao-yanzhengma"></i>
        <!-- 倒计时 -->
        <van-count-down
          :time="1000 * 60"
          format="ss s"
          slot="button"
          v-if="isCountDownShow"
          @finish="isCountDownShow = false"
        />
        <!-- 发送验证码按钮 -->
        <van-button
          v-else
          slot="button"
          round
          class="send-sms-btn"
          @click="onSendSms"
          native-type="button"
        >
          发送验证码
        </van-button>
      </van-field>
      <div style="margin: 16px">
        <van-button round block type="info" native-type="submit">
          提交
        </van-button>
      </div>
    </van-form>
  </div>
</template>

<script>
import { login, sendSms } from '../../api/user.js'
import { mapMutations } from 'vuex'
export default {
  name: 'LoginIndex',
  components: {

  },
  data () {
    return {
      // 用户登录参数
      user: {
        mobile: '15282133198',
        code: '246810'
      },
      // 表单校验规则
      userFormRules: {
        mobile: [
          { required: true, message: '请填写用户名' },
          {
            pattern: /^1[3|5|7|8]\d{9}$/, message: '手机号格式错误'
          }
        ],
        code: [
          { required: true, message: '验证码不能为空' },
          { pattern: /^\d{6}$/, message: '验证码格式错误' }]
      },
      // 发送验证码和倒计时切换
      isCountDownShow: false
    }
  },
  created () {

  },
  methods: {
    ...mapMutations(['setUser']),
    // 表单提交事件
    async onSubmit () {
      try {
        this.$toast.loading({
          message: '加载中...',
          forbidClick: true,
          duration: 0
        })
        const { data: { data } } = await login(this.user) // 双重解构
        this.$store.commit('setUser', data)
        this.$toast.success('登录成功')
        this.$router.back()
      } catch (e) {
        console.dir(e)
        if (e.response.status === 400) {
          this.$toast.fail('手机号或者验证码错误')
        } else {
          this.$toast.fail('请稍后重试')
        }
      }
    },
    // 验证手机号 通过就 显示倒计时
    async onSendSms () {
      try {
        await this.$refs.loginFormRef.validate('mobile')
        // 显示倒计时
        this.isCountDownShow = true
        // 发送请求
        await sendSms(this.user.mobile)
        console.log('验证通过')
      } catch (e) {
        console.error(e)
        // 判断
        if (e.response) {
          if (e.response.status === 429) {
            this.$toast.fail('访问过于频繁')
          } else {
            this.$toast.fail('服务器报错')
          }
        } else {
          console.log('验证不通过')
        }
      }
    }
  }
}
</script>

<style lang="less">
.toutiao {
  font-size: 37px;
}
.send-sms-btn {
  width: 192px;
  height: 46px;
  line-height: 46px;
  background-color: #ededed;
  font-size: 22px;
  color: #666;
}
.login-btn-wrap {
  padding: 53px 33px;
  .login-btn {
    background-color: #6db4fb;
    border: none;
  }
}
</style>
